<?php 
session_start( );
?>
<?
require_once('connections/mysqlDB.php');
require('smarty_connect.php');
$dbConn = new mysqli(HOST,USER,PWD,DB);
$dbConn->query("SET NAMES 'utf8'");
$smarty = new smarty_connect;
$rid = $_SESSION['rid'];
$regid = $_SESSION['regid'];
$conferenceid = $_SESSION['conferenceID'];

/* prepare statement */

//printf("action= %s, rid= %d, reginfoid= %d<br>", $_POST['action'], $_POST['rid'], $_POST['reginfoid']);
	$_SESSION['last_action']=$_POST['action'];
$action=$_POST['action'];
//var_dump($_POST);
if (isset($_POST['pastor'])) $pastor = $_POST['pastor']; else $pastor = 'N';

if ($_POST['action'] == "修改 Update") {
 if ( ($_POST['first_name'] =="") or ($_POST['last_name'] == "") ) {
 $smarty->assign('error',"英文姓名不能是空白，First & Last Name Can Not Be Blank!");
 $smarty->display('error.tpl');
 } else {
  if ($stmt = $dbConn->prepare("UPDATE registration_info SET cname=?, first_name=?, last_name=?,
      relationship=?, gender=?, groups=?, pastoral_staff=?, special_need=?, meal1=?, meal2=?, meal3=?,
      meal4=?, meal5=?, meal6=?, meal7=?, meal8=?, session1=?, session2=?, session3=?, session4=?,
      session5=?, session6=?, session7=?, session8=?, session9=?, session10=?,
      session11=?, last_update =NOW() WHERE reginfoid=?")) {
	  for ($i=1; $i < 9; $i++)
	  $meal[$i] = $_POST["meal$i"] ? 1: 0;
	  for ($i=1; $i < 12; $i++)
	  $ses[$i] = $_POST["ses$i"] ? 1: 0;  
    $stmt->bind_param('sssssisiiiiiiiiiiiiiiiiiiiii', $_POST['cname'], $_POST['first_name'], 
      $_POST['last_name'], $_POST['relation'], $_POST['gender'], $_POST['group'], $pastor,
      $_POST['special'], $meal[1], $meal[2], $meal[3], $meal[4], $meal[5], $meal[6], $meal[7],
      $meal[8], $ses[1], $ses[2], $ses[3], $ses[4], $ses[5], $ses[6], $ses[7], $ses[8], $ses[9],
      $ses[10], $ses[11], $_POST['reginfoid']);
    $stmt->execute(); 
	echo"<img src=\"wait.jpg\" width=\"444\" height=\"62\">"; 
    //printf ("Updated records: %d<br>", $stmt->affected_rows);
  }
 }
}
$rcname=$_SESSION['rlast'];
$rfirst=$_SESSION['rfirst'];
$cname = $_POST['last_name'];
$first = $_POST['first_name'];

if ($_POST['action'] == "刪除 Delete") {
  if ($stmt = $dbConn->prepare("DELETE FROM registration_info WHERE reginfoid= ?")) {
    $stmt->bind_param('i', $_POST['reginfoid']);
    $stmt->execute(); 
    if ( ($_SESSION['rlast'] == $_POST['last_name']) and ($_SESSION['rfirst'] == $_POST['first_name']) ) {
	 $_SESSION['survey']='Y';
	 }
	//echo "rcname=$rcname rfirst=$rfirst cname=$cname first=$first";
	//printf ("Deleted records: %d<br>", $stmt->affected_rows);
	echo"<img src=\"wait.jpg\" width=\"444\" height=\"62\">"; 
	
  }
}

if ($_POST['action'] == "簽證信函 Visa") {
$email=$_SESSION['email'];
$first = $_POST['first_name'];
$last = $_POST['last_name'];
include("con_genpdf.php");
}

if ($_POST['action'] == "意見 Comment") {
if ($stmt = $dbConn->prepare("INSERT INTO delregistration (regid, rid, conference_id,create_date,cname,first_name,last_name,reason) VALUES (?,?,?,NOW(),?,?,?,?)")) {
      $stmt->bind_param('iisssss', $regid,$rid,$conferenceid,$_SESSION['rcname'],$_SESSION['rfirst'],$_SESSION['rlast'],$_POST['comment']);
      $stmt->execute();
      $reginfoid = $stmt->insert_id;
	  echo"<img src=\"wait.jpg\" width=\"444\" height=\"62\">"; 
   }
}

if ($_POST['action'] == "簽證信函 VisaMC") {
$email=$_SESSION['email'];
$first = $_POST['first_name'];
$last = $_POST['last_name'];
$head = "Dear $first $last, 
<br><br>Thank you for registering Forerunner Christian Church 2009 Summer Conference. 
The conference to be held from 7/2/2009 through 7/6/2009 at San Franscisco Hyatt Hotel.<br><br>"; 

$to=$email;
$subject = "慕主特會簽證信";
$message = "
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
</head>
<body>
$head
$message
</body>
</html>";

// Always set content-type when sending HTML email

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";// More headers
$headers .= 'From:FRCC Admin<support@frccinnerlife.org>' . "\r\n";
$headers .= "\r\n";

$mail = mail($to,$subject,$message,$headers);
}

if ($_POST['action'] == "增加 Add") {
 if ( ($_POST['first_name'] =="") or ($_POST['last_name'] == "") ) {
 $smarty->assign('error',"英文姓名不能是空白，First & Last Name Can Not Be Blank!");
 $smarty->display('error.tpl');
 } else {
    $rid = $_POST['rid'];
    if ($stmt = $dbConn->prepare("INSERT INTO registration_info (rid) VALUES (?)")) {
      $stmt->bind_param('i', $rid);
      $stmt->execute();
      $reginfoid = $stmt->insert_id;
      //printf ("Added records: %d, record id: %d<br>", $stmt->affected_rows, $reginfoid);
    }

    if ($stmt = $dbConn->prepare("UPDATE registration_info SET cname=?, first_name=?, last_name=?,
      relationship=?, gender=?, groups=?, pastoral_staff=?, special_need=?, meal1=?, meal2=?, meal3=?,
      meal4=?, meal5=?, meal6=?, meal7=?, meal8=?, session1=?, session2=?, session3=?, session4=?,
      session5=?, session6=?, session7=?, session8=?, session9=?, session10=?,
      session11=?, create_date=NOW() WHERE reginfoid=?")) {
	  for ($i=1; $i < 9; $i++)
	  $meal[$i] = $_POST["meal$i"] ? 1: 0;
	  for ($i=1; $i < 12; $i++)
	  $ses[$i] = $_POST["ses$i"] ? 1: 0;  
      $stmt->bind_param('sssssisiiiiiiiiiiiiiiiiiiiii', $_POST['cname'], $_POST['first_name'], 
        $_POST['last_name'], $_POST['relation'], $_POST['gender'], $_POST['group'], $pastor,
        $_POST['special'], $meal[1], $meal[2], $meal[3], $meal[4], $meal[5], $meal[6], $meal[7],
      $meal[8], $ses[1], $ses[2], $ses[3], $ses[4], $ses[5], $ses[6], $ses[7], $ses[8], $ses[9],
      $ses[10], $ses[11], $reginfoid);
      $stmt->execute(); 
      //printf ("Updated records: %d<br>", $stmt->affected_rows);
	  echo"<img src=\"wait.jpg\" width=\"444\" height=\"62\">"; 
    }
  }
}

if ($_POST['action'] == "xAdd") {
    if ($stmt = $dbConn->prepare("INSERT INTO registration_info (rid, cname, first_name, last_name,
      relationship, gender, groups, pastoral_staff, meal1, meal2, meal3, meal4, meal5, meal6,
      meal7, meal8) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")) {
    $stmt->bind_param('isssssisiiiiiiii', $_POST['rid'], $_POST['cname'], $_POST['first_name'],
      $_POST['last_name'], $_POST['relation'], $_POST['gender'], $_POST['group'], $_POST['pastor'],
      $_POST['meal1'], $_POST['meal2'], $_POST['meal3'], $_POST['meal4'], $_POST['meal5'],
      $_POST['meal6'], $_POST['meal7'], $_POST['meal8']);
    $stmt->execute();
    printf ("Added records: %d<br>", $stmt->affected_rows);
  }
}

if ($stmt = $dbConn->prepare("UPDATE registration SET last_update = NOW() 
	WHERE rid = ? and regid = ? ")) {
    $stmt->bind_param('ii', $rid, $regid);
    $stmt->execute(); 
    //printf ("Updated records: %d<br>", $stmt->affected_rows);

  }
  
$dbConn->close();
?>

<script>
<!--
setTimeout("window.location.replace('con_registration.php')",3500);
-->
</script>
